let fm = new Vue({
    el: '#film-list',
    data: {
        films: [],
        currentFilm:{},
        isShow: false,
        currentPage: '',
        totalPageCount:''
    },
    created() {
        $.get('http://localhost:8080/cinema/api/FilmListServlet.do', function (dto) {
            fm.films = dto.value;
        });
    },
    methods: {
        del: function (i, id, status) {
            if (status == 1) {
                layer.msg("该影片已上映，无法进行删除操作", { offset: 't',icon: 4 });
                return;
            }

            layer.confirm('确定要删除此影片嘛？', {skin: 'layui-layer-molv',icon:3,title:'提示',
                btn: ['是的', '取消']
            }, function () {
                let formData = {
                    filmId: id
                }
                $.ajax({
                    url: 'http://localhost:8080/cinema/api/FilmDeleteServlet.do',
                    type: 'delete',
                    data: JSON.stringify(formData),
                    dataType: 'json',
                    success: function (dto) {
                        if (dto.code == 200) {
                            fm.films.splice(i, 1);
                            layer.msg(dto.value, { icon: 1 });
                        } else {
                            layer.msg(dto.msg, { icon: 5 });
                        }
                    }
                });

            }, function () {

            });
        },
        update: function (f) {
            if (f.filmStatus == 1) {
                layer.msg("该影片已上映，无法进行修改操作", { offset: 't',icon: 4 });
                return;
            }
            this.currentFilm=f;
            layer.open({
				type: 1,
				title: '修改影片',
				anim: 1,   // 动画
				resize: false,  // 是否允许拉伸改变宽高
				area: ['800px', '480px'], // 设置宽，高
				shadeClose: false, //点击遮罩关闭
				scrollbar: false,   // 屏蔽浏览器滚动（false: 则屏蔽）
				content: $("#film-update"),
				btn: ['确认', '取消'],
				btn1: function (index) {
					layer.close(index);
					
				},
				btn2: function () {
					
				}
			});
        },
        checkPager: function () {
            let btn = $("#check");
            if (btn.text() == "分页显示") {
                // 分页查询
                $.get('http://localhost:8080/cinema/api/FilmListByPagerServlet.do', function (dto) {
                    fm.films = dto.value.data;
                    fm.isShow = true;
                    fm.currentPage = dto.value.currentPage;
                    btn.text('显示全部');    

                    if (dto.value.totalRecords % dto.value.pageCount == 0) {
                        fm.totalPageCount = Math.floor(dto.value.totalRecords / dto.value.pageCount);
                      
                    } else {
                        fm.totalPageCount = Math.floor(dto.value.totalRecords / dto.value.pageCount)+1;
                       
                    }

                });
            } else {
                // 非分页查询
                $.get('http://localhost:8080/cinema/api/FilmListServlet.do', function (dto) {
                    fm.films = dto.value;
                    fm.isShow = false;
                    btn.text('分页显示');
                });
            }
        },
        first: function () {
            this.currentPage=1;
            let formData = {
                cp: this.currentPage
            }
            $.get('http://localhost:8080/cinema/api/FilmListByPagerServlet.do', formData,function (dto) {
                fm.films = dto.value.data;
            
            });
        },
        up: function () {
            if(this.currentPage==1){
                return;
            }
            this.currentPage=this.currentPage-1;
            let formData = {
                cp:this.currentPage
            }
            $.get('http://localhost:8080/cinema/api/FilmListByPagerServlet.do', formData,function (dto) {
                fm.films = dto.value.data;
               
            });
        },
        down: function () {
            if(this.currentPage==this.totalPageCount){
                return;
            }
            this.currentPage=this.currentPage+1;
            let formData = {
                cp: this.currentPage
            }
            $.get('http://localhost:8080/cinema/api/FilmListByPagerServlet.do', formData,function (dto) {
                fm.films = dto.value.data;
        
            });
        },
        end: function () {
            let formData = {
                cp:this.totalPageCount
            }
            $.get('http://localhost:8080/cinema/api/FilmListByPagerServlet.do', formData,function (dto) {
                fm.films = dto.value.data;
                fm.currentPage = dto.value.currentPage;
            });
        },
        go:function(){
            let formData={
                cp:$(".bb").val()
            }
            
            $.get('http://localhost:8080/cinema/api/FilmListByPagerServlet.do', formData,function (dto) {
                fm.films = dto.value.data;
                fm.currentPage = dto.value.currentPage;
            });
        },
        selectByLike:function(){
            
            let formData={
                cond:$("#cond").val()
            }
            // 发送异步 ，实现影片的模糊查询
            $.get('http://localhost:8080/cinema/api/SelectFilmsByLikeServlet.do',formData,function(dto){
                fm.films=dto.value;
            });
        }

    }
});